# -*- coding: utf-8 -*-

import tkinter as tk
from tkinter import messagebox
import openpyxl
import pyttsx3
import time

# 初始化朗读引擎
#engine = pyttsx3.init()
# 预加载TTS引擎
engine = pyttsx3.init()
engine.setProperty('rate', 150)
engine.say('      ')  # 播放空内容初始化
engine.runAndWait()
# 加载Excel文件和工作表
file_path = "namelist.xlsx"
workbook = openpyxl.load_workbook(file_path)
sheet = workbook["点名"]

# 初始化当前行
current_row = 6

def update_ui():
    global current_row
    
    # 获取C列的单元格内容
    cell_value = sheet[f'C{current_row}'].value
    
    if cell_value is None:
        messagebox.showinfo("结束", "已处理完所有数据")
        root.destroy()
        return
    
    # 更新UI显示
    label.config(text=cell_value)
    
    # 等待用户输入
    entry.config(state=tk.NORMAL)
    entry.delete(0, tk.END)
    entry.focus()

    # 延迟朗读（确保内容先显示）
    root.after(500, lambda: speak_text('嗯'+cell_value))

def speak_text(text):
    """朗读文本"""
    engine.say(text)
    engine.runAndWait()

def on_enter(event):
    global current_row
    
    user_input = entry.get()
    
    if user_input not in ['0', '1']:
        messagebox.showerror("错误", "请输入0或1")
        return
    
    # 将输入存储到G列
    sheet[f'G{current_row}'] = int(user_input)
    
    # 保存Excel文件
    workbook.save(file_path)
    
    # 移动到下一行
    current_row += 1
    
    # 更新UI
    update_ui()

def close_window():
    """关闭窗口"""
    root.destroy()

def minimize_window():
    """最小化窗口"""
    root.iconify()

# 创建主窗口
root = tk.Tk()
root.title("密码学学生记分册")

# 设置全屏显示
root.attributes("-fullscreen", True)

# 创建标签显示单元格内容
label = tk.Label(root, text="", font=("Arial", 48))  # 放大字体
label.pack(expand=True, pady=100)

# 创建输入框
entry = tk.Entry(root, font=("Arial", 24), state=tk.DISABLED)
entry.pack(pady=20)
entry.bind("<Return>", on_enter)

# 创建关闭和最小化按钮
button_frame = tk.Frame(root)
button_frame.pack(side=tk.BOTTOM, pady=10)

close_button = tk.Button(button_frame, text="关闭", font=("Arial", 16), command=close_window)
close_button.pack(side=tk.LEFT, padx=10)

minimize_button = tk.Button(button_frame, text="最小化", font=("Arial", 16), command=minimize_window)
minimize_button.pack(side=tk.LEFT, padx=10)

# 初始化UI
update_ui()

# 运行主循环
root.mainloop()